<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改字典' : '新增字典'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form :model="form" ref="form" :rules="rules" label-width="100px">
      <el-form-item label="上级字典">
        <dic-select
          :disabled="parentSelect"
          ref="dicSelect"
          class="w393-mr20 fl w440"
          :selectValue="form.parentId"
          v-model="form.parentId"
          @select="handleDeptSelect"
        />
        <el-checkbox v-model="parentSelect" @change="checkChange">顶级菜单</el-checkbox>
      </el-form-item>
      <el-divider />

      <el-form-item label="字典显示名" prop="dictionariesShowName">
        <el-input
          class="short-input"
          type="text"
          autocomplete="false"
          placeholder="请输入"
          v-model="form.dictionariesShowName"
        />
      </el-form-item>
      <!-- 
      <el-form-item label="字典编码" prop="dictionariesCode">
        <el-input type="text" placeholder="请输入" v-model="form.dictionariesCode" />
      </el-form-item>-->

      <el-form-item label="说明">
        <el-input type="textarea" placeholder="请输入" v-model="form.remark" allowClear />
      </el-form-item>
      <el-form-item label="排序" prop="dictionariesOrder">
        <el-input-number :min="0" :max="100" placeholder="请输入排序值" v-model="form.dictionariesOrder" />
      </el-form-item>
      <el-form-item label="允许删除" v-if="form.allowDel">
        <el-radio-group v-model="form.allowDel">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="handleCancel">取消</el-button>
      <el-button @click="handleOk" type="primary">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import DicSelect from './DicSelect'
import { updateDic, insertDic } from '@/api/dictionary'
import { openNotification, removeSession } from '@/utils/util'
import pick from 'lodash.pick'

export default {
  components: {
    DicSelect
  },
  data() {
    return {
      parentSelect: true,
      visible: false,
      isEdit: false,
      form: {
        dictionariesName: null,
        dictionariesShowName: null,
        dictionariesCode: null,
        remark: null,
        dictionariesOrder: 0
      },
      rules: {
        dictionariesShowName: [{ required: true, message: '请输入字典显示名称', trigger: 'blur' }],
        dictionariesOrder: [
          {
            type: 'number',
            message: '排序值必须为数字'
          }
        ]
      }
    }
  },
  methods: {
    handleDeptSelect(value) {
      this.$set(this.form, 'parentId', value.key)
    },
    checkChange(e) {
      if (e) {
        this.reset()
      } else {
        this.parentSelect = false
      }
    },
    reset() {
      this.$nextTick(() => {
        this.$set(this.form, 'parentId', null)
        this.$refs.dicSelect.reset()
        this.parentSelect = true
      })
    },
    edit(bool, record) {
      this.isEdit = bool
      this.form = Object.assign({}, record)
      // 开始选择部门，后面取消掉选择的情况
      if (!record.parentId) {
        this.reset()
      } else {
        this.parentSelect = false
      }
      if (!bool) {
        this.$set(this.form, 'dictionariesOrder', 0)
      }
      this.visible = true
    },
    async handleOk(e) {
      e.stopPropagation()
      this.$refs.form.validate(async valid => {
        if (valid) {
          let mes = '',
            res = ''
          if (this.isEdit) {
            mes = '修改操作成功'
            res = await updateDic(this.form)
          } else {
            mes = '新增操作成功'
            res = await insertDic(this.form)
          }
          if (res.state === '000000') {
            removeSession('dictionaryData')
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    },
    handleCancel() {
      this.visible = false
    }
  }
}
</script>

<style scoped>
.w440 {
  width: 440px !important;
}
</style>
